﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace O1.IO.Storage
{
    internal class TransactionLogEnlistment : IEnlistmentNotification
    {
        private readonly TransactionLog log;

        private readonly byte[] nextVersion;

        public TransactionLogEnlistment(TransactionLog log, byte[] nextVersion)
        {
            this.log = log;
            this.nextVersion = nextVersion;
        }

        public void Commit(Enlistment enlistment)
        {
            this.log.CommitEntry();
            enlistment.Done();
        }

        public void InDoubt(Enlistment enlistment)
        {
        }

        public void Prepare(PreparingEnlistment preparingEnlistment)
        {
            this.log.WriteToLog(this.nextVersion);
            preparingEnlistment.Prepared();
        }

        public void Rollback(Enlistment enlistment)
        {
            this.log.InvalidateEntry();
            enlistment.Done();
        }
    }
}
